package com.huatai.bi.xxljob;

import com.huatai.bi.entity.DatasourceEntity;
import com.huatai.bi.service.DatasourceService;
import com.huatai.bi.service.TableService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @BelongsProject: itc-airport-cloud
 * @BelongsPackage: com.itc.bi.xxljob
 * @Author: xuzy
 */
@Slf4j
@Component
public class DataSourceXxlJob {
	@Autowired
	private DatasourceService datasourceService;
	@Autowired
	TableService tableService;

	/**
	 * sql数据同步方法
	 * @throws Exception
	 */
	@XxlJob("datasourceHandler")
	public void datasourceHandler()throws Exception{
		log.debug("xxl更新数据源下表结构信息-开始");
		List<DatasourceEntity> list = datasourceService.list();
		list.stream().forEach( source -> {
			try {
				tableService.updateByDatasourceId(source.getId());
			}catch (Exception e){
				log.error("更新数据源{}失败,异常信息:{}",source.getId(),e.getMessage());
			}
		});
		log.debug("xxl更新数据源下表结构信息-结束");
	}
}
